package com.bawei.cashier

import android.util.Log
import android.view.LayoutInflater
import android.widget.CheckBox
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.base.bean.PayerDetailEntity
import com.bawei.base.bean.payerDetail2Entity
import com.bawei.base.net.ApiService
import com.bawei.cashier.databinding.ItemPayerdetail2Binding
import com.bawei.cashier.databinding.ItemPayerdetailBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.coroutines.launch
import javax.inject.Inject

class CashierDetailAdapter @Inject constructor(
    var apiService: ApiService,
    var lifecycleOwner: LifecycleOwner
) :
    BaseQuickAdapter<PayerDetailEntity, BaseViewHolder>(R.layout.item_payerdetail) {
    companion object {
         var cashierDetailAdapter2= CashierDetailAdapter2()
    }

    override fun convert(helper: BaseViewHolder, item: PayerDetailEntity) {
        helper.setText(R.id.payerdetail_pirce, item.totalAmount.toString())
        helper.setText(R.id.payerdetail_time, item.paymentTime)
        lifecycleOwner.lifecycleScope.launch {
            try {
                val split = item.paymentItemIds.split(",")
                cashierDetailAdapter2 = CashierDetailAdapter2()
                val data1 = apiService.payDetailById2(split[1].toInt()).data
                val data2 = apiService.payDetailById2(split[2].toInt()).data
                val data3 = apiService.payDetailById2(split[0].toInt()).data
                cashierDetailAdapter2.data.add(data1)
                cashierDetailAdapter2.data.add(data2)
                cashierDetailAdapter2.data.add(data3)
                val bind = ItemPayerdetailBinding.bind(helper.itemView)
                bind.payerdetailRec2.adapter = cashierDetailAdapter2
                bind.payerdetailRec2.layoutManager = LinearLayoutManager(context)
            } catch (e: Exception) {
                Log.d("CashierDetailAdapter", "" + e.message)
            }

        }
    }
}

class CashierDetailAdapter2 :
    BaseQuickAdapter<payerDetail2Entity, BaseViewHolder>(R.layout.item_payerdetail2) {
    override fun convert(helper: BaseViewHolder, item: payerDetail2Entity) {
        helper.setText(R.id.payerdetail2_title, item.chargeType)
        helper.setText(R.id.payerdetail2_pirec, item.receivableAmount.toString())
        helper.setText(R.id.payerdetail2_time, item.paymentTime)
        val view = helper.getView<CheckBox>(R.id.payerdetail2_check)
        view.isChecked = item.isCheck
    }

}